var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();

Page({
  data: {
    ganzhi: {},

    id: 0,
    news_detail: {},
    news_index: 0,
    news: {},
    news_seed: 0,

    comment_list1: [],
    re_name: '',
    to_cid: 0,  // 回复评论id
    content: '',
    release_focus: false,

    preload_list: []
  },
  onLoad(options) {
    this.setData({ ganzhi: app.get_ganzhi() });

    this.data.id = parseInt(options.id);
    this.newsDetail(() => {
      this.setData({ news: this.data.news_detail.slide[this.data.news_index] });
      this.data.news_index++;

      this.roll();

      app.rich_handle(this.data.news_detail.content, WxParse, this);
    });

    // this.newsList();

    this.comment_list();
  },
  onUnload() {
    clearInterval(this.data.news_seed);
  },
  roll() {
    this.data.news_seed = setInterval(() => {
      this.setData({ news: this.data.news_detail.slide[this.data.news_index] });
      if (this.data.news_index !== this.data.news_detail.slide.length - 1) {
        this.data.news_index++;
      } else {
        this.data.news_index = 0;
      }
    }, 2000);
  },
  // 获取新闻详情
  newsDetail(callback) {
    app.ajax('index/newsDetail', { news_id: this.data.id }, res => {
      app.format_img(res);
      app.format_img_arr(res.slide);

      // 预加载
      let img_arr = app.get_column(res.slide, 'pic');
      this.setData({ preload_list: this.data.preload_list.concat(img_arr) });

      this.setData({ news_detail: res });
      wx.setNavigationBarTitle({ title: res.title });

      if (callback) {
        callback();
      }
    });
  },
  // 去新闻详情
  to_news_detail(e) {
    if (e.currentTarget.dataset.url) {
      wx.redirectTo({ url: '/pages/zaji-detail/zaji-detail?id=' + e.currentTarget.dataset.url });
    }
  },
  // 评论列表
  comment_list() {
    app.ajax('index/commentList', { news_id: this.data.id }, (res) => {
      app.format_img_arr(res, 'avatar');
      for (let i = 0; i < res.length; i++) {
        app.format_img_arr(res[i].child, 'avatar');
      }
      this.setData({ comment_list1: res });
    });
  },
  bind_focus(e) {
    this.setData({ input_bottom: e.detail.height });
  },
  bind_input(e) {
    app.bind_input(e, this);
  },
  show_input(e) {
    let re_user = e.currentTarget.dataset.re_user;
    if (re_user) {
      this.setData({
        to_cid: re_user.id,
        re_name: re_user.nickname,
        release_focus: true
      });
    } else {
      this.setData({
        to_cid: 0,
        release_focus: true
      });
    }
  },
  hide_input() {
    this.setData({ release_focus: false });
  },
  comment_add() {
    let post = {
      news_id: this.data.id,
      content: this.data.content,
    };

    if (this.data.to_cid !== 0) {
      post.to_cid = this.data.to_cid;
    }

    app.ajax('index/commentAdd', post, () => {
      app.toast('已评论');
      this.setData({ content: '' });
      this.comment_list();
    });
  },
  roll_ts() {
    clearInterval(this.data.news_seed);
  },
  roll_te() {
    this.roll();
  },
  wxParseTagATap(e) {
    let id = e.currentTarget.dataset.src;
    wx.navigateTo({ url: '/pages/news-detail/news-detail?id=' + id.substr(1) });
  }
});